//
//  YXCustomCellViewController.m
//  YXImageBrowserDemo
//
//  Created by guo peng huang on 2021/5/19.
//

#import "YXCustomCellViewController.h"
#import "YXImageBrowser.h"
#import "YXCustomData.h"

static NSString *kAdvertKey = @"自定义cell";

@interface YXCustomCellViewController () <YXImageBrowserDelegate>

@end

@implementation YXCustomCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

+ (NSString *)yx_title {
    return @"自定义cell";
}

- (void)initData {
    NSMutableArray *array = [NSMutableArray array];
    [array addObjectsFromArray:[YXBaseFileManager yx_imageURLs]];
    [array addObject:kAdvertKey];
    self.dataArray = array;
}

#pragma mark - <YXImageBrowserDelegate>

- (void)yx_imageBrowser:(YXImageBrowser *)imageBrowser pageChanged:(NSInteger)page data:(id<YXDataProtocol>)data {
    // 当是自定义的 Cell 时，隐藏右边的操作按钮
    // 对于工具栏的处理自定义一个 id<YBIBToolViewHandler> 是最灵活的方式，默认实现很多时候可能满足不了需求
    imageBrowser.defaultToolViewHandler.topView.operationButton.hidden = [data isKindOfClass:YXCustomData.self];
}

#pragma mark - override

- (void)yx_selectedIndex:(NSInteger)index {
    
    NSMutableArray *datas = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqualToString:kAdvertKey]) {
            
            // 自定义的广告 Cell
            YXCustomData *data = [YXCustomData new];
            data.text = @"生活不止眼前的苟且,\n还有诗和远方!";
            [datas addObject:data];
            
        } else {
            
            // 网络图片
            YXImageData *data = [YXImageData new];
            data.imageURL = [NSURL URLWithString:obj];
            data.projectiveView = [self viewAtIndex:idx];
            [datas addObject:data];
            
        }
    }];
    
    YXImageBrowser *browser = [YXImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = index;
    browser.delegate = self;
    [browser show];
}


@end
